/*
 * Copyright (C) 2004, Velocityme Partnership
 * ContactableValueActionForm.java
 *
 * Created on 28 September 2004, 07:40
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.valueobjects.ContactDetailTypeValue;
import com.velocityme.valueobjects.ContactDetailValue;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class ContactableValueActionForm extends NodeValueActionForm {
    
    private class ContactDetail implements Serializable {
        Integer m_detailId;
        String m_typeName;
        String m_value;
    }
    
    private Map m_contactDetailMap;
    private DecimalFormat m_decimalFormat = new DecimalFormat("000");
    
    /** Creates a new instance of ContactableValueActionForm */
    public ContactableValueActionForm() {
        m_contactDetailMap = new TreeMap();
    }
    
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        m_contactDetailMap.clear();
    }
    
    public Collection getContactDetails() {
        return m_contactDetailMap.keySet();
    }
    
    public void setContactDetailTypeValues(Collection contactDetailTypeValues) {
        Iterator i = contactDetailTypeValues.iterator();
        while(i.hasNext()) {
            ContactDetailTypeValue contactDetailTypeValue = (ContactDetailTypeValue)i.next();
            String key = m_decimalFormat.format(contactDetailTypeValue.getSequenceNumber().intValue());
            m_contactDetailMap.put(key, new ContactDetail());
            ((ContactDetail)m_contactDetailMap.get(key)).m_detailId = null;
            ((ContactDetail)m_contactDetailMap.get(key)).m_value = "";
            ((ContactDetail)m_contactDetailMap.get(key)).m_typeName = contactDetailTypeValue.getName();
        }
    }
    
    public ContactDetailValue[] getContactDetailValues(Collection contactDetailTypeValues) {
        ContactDetailValue[] contactDetailValues = new ContactDetailValue[m_contactDetailMap.size()];
        Map contactDetailTypeMap = new HashMap();
        Iterator t = contactDetailTypeValues.iterator();
        while(t.hasNext()) {
            ContactDetailTypeValue contactDetailTypeValue = (ContactDetailTypeValue)t.next();
            String key = m_decimalFormat.format(contactDetailTypeValue.getSequenceNumber().intValue());
            contactDetailTypeMap.put(key, contactDetailTypeValue);
        }
        Iterator i = m_contactDetailMap.keySet().iterator();
        int index = 0;
        while(i.hasNext()) {
            String key = (String)i.next();
            ContactDetail contactDetail = (ContactDetail)m_contactDetailMap.get(key);
            ContactDetailTypeValue contactDetailTypeValue = (ContactDetailTypeValue)contactDetailTypeMap.get(key);
            contactDetailValues[index] = new ContactDetailValue();
            if (contactDetail.m_detailId != null)
                contactDetailValues[index].setContactDetailId(contactDetail.m_detailId);
            contactDetailValues[index].setValue(contactDetail.m_value);
            contactDetailValues[index].setContactDetailTypeValue(contactDetailTypeValue);
            index++;
        }
        return contactDetailValues;
    }
    
    public Integer getContactDetailId(String key) {
        return ((ContactDetail)m_contactDetailMap.get(key)).m_detailId;
    }
    
    public void setContactDetailId(String key, Integer contactDetailId) {
        if (!m_contactDetailMap.containsKey(key))
            m_contactDetailMap.put(key, new ContactDetail());
        ((ContactDetail)m_contactDetailMap.get(key)).m_detailId = contactDetailId;
    }
    
    public String getContactDetailValue(String key) {
        return ((ContactDetail)m_contactDetailMap.get(key)).m_value;
    }
   
    public void setContactDetailValue(String key, String value) {
        if (!m_contactDetailMap.containsKey(key))
            m_contactDetailMap.put(key, new ContactDetail());
        ((ContactDetail)m_contactDetailMap.get(key)).m_value = value;
    }
    
    public String getContactDetailTypeName(String key) {
        return ((ContactDetail)m_contactDetailMap.get(key)).m_typeName;
    }
   
}
